package org.liujing.jsw.client.border;

import org.liujing.jsw.client.ui.*;
import org.liujing.jsw.client.dom.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.*;
import com.google.gwt.dom.client.Style;
import com.google.gwt.core.client.GWT;
import java.util.logging.*;

@SuppressWarnings("deprecation")
public class BaseImageBorder extends AbstractBorder{
	private static Logger log = Logger.getLogger("client.border.BaseImageBorder");
	private Insets insets;
	private int minWidth = 10;
	private int minHeight = 10;
	private Element[] elements = new Element[8];
	private BordImageBundle images;
	
	public BaseImageBorder(BordImageBundle images, Insets insets, int minWidth, int minHeight){
		this.insets = insets;
		this.images = images;
		this.minWidth = minWidth;
		this.minHeight = minHeight;
		for(int i= 0; i<8; i++){
			elements[i] = DOM.createDiv();
			elements[i].getStyle().setPosition(Style.Position.ABSOLUTE);
		}
		
		images.getTopLeft().applyTo(elements[0]);
		images.getTop().applyTo(elements[1]);
		images.getTopRight().applyTo(elements[2]);
		images.getLeft().applyTo(elements[3]);
		images.getRight().applyTo(elements[4]);
		images.getBottomLeft().applyTo(elements[5]);
		images.getBottom().applyTo(elements[6]);
		images.getBottomRight().applyTo(elements[7]);
		
		DomUtil.setElementBound(elements[0], 0, 0, images.getTopLeft().width, images.getTopLeft().height);
		DomUtil.setElementPos(elements[1], images.getTopLeft().width, 0);
		DomUtil.setElementSize(elements[2], images.getTopRight().width, images.getTopRight().height);
		DomUtil.setElementPos(elements[3], 0, images.getTopLeft().height);
		
		
		
		
	}
	
	public Insets getBorderInsets(Component c){
		return insets;
	}
	
	public void createBorder(Component c, int x, int y, int width, int height) {
		for(int i= 0; i<8; i++){
			c.getDomElement().appendChild(elements[i]);
		}
		resize( c, width, height);
	}
	    
	public void removeBorder(Component c){
		for(int i= 0; i<8; i++){
			c.getDomElement().removeChild(elements[i]);
		}		
	}
	
	public void resize(Component c, Dimension size){
		resize(c, size.width, size.height);
	}
	
	private void resize(Component c, int width, int height){
		log.fine("width = " + width);
		if(width <= minWidth || height <= minHeight)
			return;
		DomUtil.setElementSize(elements[1], width - images.getTopLeft().width - images.getTopRight().width, images.getTopLeft().height);
		
		DomUtil.setElementBound(elements[2], width - images.getTopRight().width, 0, images.getTopRight().width, images.getTopRight().height);
		DomUtil.setElementSize(elements[3], images.getLeft().width, height - images.getTopLeft().height - images.getBottomLeft().height);
		DomUtil.setElementBound(elements[4], width - images.getRight().width, images.getTopRight().height, images.getRight().width, height - images.getTopRight().height - images.getBottomRight().height);
		DomUtil.setElementBound(elements[5], 0, height - images.getBottomLeft().height, images.getBottomLeft().width, images.getBottomLeft().height);
		DomUtil.setElementBound(elements[6], images.getBottomLeft().width, height - images.getBottom().height, width - images.getBottomLeft().width - images.getBottomRight().width, images.getBottom().height);
		DomUtil.setElementBound(elements[7], width - images.getBottomRight().width, height - images.getBottomRight().height, images.getBottomRight().width, images.getBottomRight().height);
		
	}
}
